package com.xyzzlky.test;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;

import com.bytedance.sdk.openadsdk.BuildConfig;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;

import org.cocos2d.demo.R;

import java.util.HashMap;

public class BU {

    private static TTAdManager mTTMgr = null;
    private static Activity activity;
    private static HashMap<String, BURewardedVideoAd> rvAds = new HashMap<>();
    private static Java2Js java2js;

    public static void initSDK(Context context, String appId) {
        if (TextUtils.isEmpty(appId)) {
            return;
        }
        TTAdConfig ttAdConfig = new TTAdConfig.Builder()
                .appId(appId)
                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                .appName(context.getResources().getString(R.string.app_name))
                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
                .allowShowNotify(true) //是否允许sdk展示通知栏提示
                .allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
                .debug(BuildConfig.DEBUG) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
                .directDownloadNetworkType(
                        TTAdConstant.NETWORK_STATE_WIFI,
                        TTAdConstant.NETWORK_STATE_MOBILE
                ) //允许直接下载的网络状态集合
                .supportMultiProcess(true) //是否支持多进程
                .needClearTaskReset() //.httpStack(new MyOkStack3())//自定义网络库，demo中给出了okhttp3版本的样例，其余请自行开发或者咨询工作人员。
                .build();
        mTTMgr = TTAdSdk.init(context, ttAdConfig);
    }

    public static TTAdNative getTTAdNative(Context context) {
        if (null == mTTMgr) {
            return null;
        }
        return mTTMgr.createAdNative(context);
    }

    public static TTAdManager getTTMgr() {
        return mTTMgr;
    }

    public static void setJSCallback(Java2Js java2js) {
        BU.java2js = java2js;
    }

    public static void setCurActivity(Activity activity) {
        BU.activity = activity;
    }

    public static boolean createRewardedVideoAd(String adUnitId) {
        if (null != mTTMgr) {
            if (null != rvAds.get(adUnitId)) {
                return true;
            }
            BURewardedVideoAd ad = new BURewardedVideoAd(activity, adUnitId, new AdRes(adUnitId));
            rvAds.put(adUnitId, ad);
            // 预加载
            ad.load(false);
            return true;
        }
        return false;
    }


    public static void loadRewardedVideoAd(String adUnitId) {
        rvAds.get(adUnitId).load(true);

    }

    public static void showRewardedVideoAd(String adUnitId) {
        rvAds.get(adUnitId).show();
    }

    private static class AdRes implements AdListener {

        private String adUnitId;

        public AdRes(String adUnitId) {
            this.adUnitId = adUnitId;
        }

        @Override
        public void loadResult(boolean result) {
            if (null != java2js) {
                java2js.callJs("zz.loadRVResult(\"" + adUnitId + "\"," + result + ");");
            }
        }

        @Override
        public void showResult(boolean result) {
            if (null != java2js) {
                java2js.callJs("zz.showRVResult(\"" + adUnitId + "\"," + result + ");");
            }
        }

        @Override
        public void onLoad() {
            if (null != java2js) {
                java2js.callJs("zz.onRVLoad(\"" + adUnitId + "\");");
            }
        }

        @Override
        public void onError(int errCode, String errMsg) {
            if (null != java2js) {
                String baseMsg = Base64.encodeToString(errMsg.getBytes(), Base64.NO_WRAP);
                java2js.callJs("zz.onRVError(\"" + adUnitId + "\"," + errCode + ",\"" + baseMsg + "\");");
            }
        }

        @Override
        public void onClose(boolean reward) {
            if (null != java2js) {
                java2js.callJs("zz.onRVError(\"" + adUnitId + "\"," + reward + ");");
            }
        }
    }
}
